package com.sky.controller.admin;

import com.sky.properties.AliyunOOSProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
@RequestMapping("/admin/common/upload")
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping
    public Result<String> upload(MultipartFile file){
        log.info("文件上传:{}",file.getOriginalFilename());
        try {
            byte[] bytes = file.getBytes();
            String url = aliyunOSSOperator.upload(bytes, file.getOriginalFilename());
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传文件失败");
        }
    }
}
